﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Entity
{
    public class AnimationSprite:Sprite
    {
        public Point _frameSize;
        protected Point _sheetSize;
        Point _religionPos = new Point();
        int[] _animateNumber;
        int _frameCounter;

        protected int _deltaTime;
        protected int _frameTime;
        protected Point _currentFrame;
        protected SpriteEffects _effect;

        Color _animationColor;

        public int DeltaTime
        {
            get{return _deltaTime;}
            set{_deltaTime = value;}
        }

        public bool IsReplay
        {
            get;
            set;
        }

        public SpriteEffects DrawEffect
        {
            get { return _effect; }
            set { _effect = value; }
        }

        public AnimationSprite() 
        {
            Initialize();
        }

        public void Initialize() 
        {
            _deltaTime = 50;
            _frameTime = 0;
            _currentFrame = new Point();
            IsReplay = true;
            Alpha = 1;
            Rotation = 0;
            Color = Color.White;
            Scale = new Microsoft.Xna.Framework.Vector2(1);
            IsReplay = true;
            DrawEffect = SpriteEffects.None;
        }

        public void SetAnimation(Point sheetSize, int[] animateNumber)
        {
            this._animateNumber = animateNumber;
            this._sheetSize = sheetSize;
            _frameSize = new Point(_texture.Width / sheetSize.X, _texture.Height / sheetSize.Y);
        }

        public void SetChangeFrameAnimation(int[] animateNumber)
        {
            this._animateNumber = animateNumber;
        }

        public void SetAnimation(Point sheetSize)
        {
            int[] array = new int[sheetSize.X * sheetSize.Y];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = i;
            }
            this._animateNumber = array;
            this._sheetSize = sheetSize;
            _frameSize = new Point(_texture.Width / sheetSize.X, _texture.Height / sheetSize.Y);
        }

        protected override void DrawSelf(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //base.DrawSelf(spriteBatch, gameTime);
            _animationColor = Color * Alpha;
            Rectangle source;
            _religionPos.X = _animateNumber[_frameCounter] % _sheetSize.X;
            _religionPos.Y = _animateNumber[_frameCounter] / _sheetSize.X;

            if (_sheetSize.X == 1 && _sheetSize.Y == 1)
                _religionPos = new Point(0,0);

            source = new Rectangle(_religionPos.X* _frameSize.X,
                              _religionPos.Y * _frameSize.Y,
                              _frameSize.X, _frameSize.Y);
            Vector2 bbg = new Vector2(_frameSize.X, _frameSize.Y);
            //Origin = bbg;


            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Projection);
            spriteBatch.Draw(_texture, Position,
             source, _animationColor, -Rotation, Origin*bbg, Scale, DrawEffect, 0);
            spriteBatch.End();


        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            _frameTime += delta;
            if (IsReplay && _frameTime > _deltaTime)
            {
                if (_frameCounter >= _animateNumber.Length-1) _frameCounter = 0;
                else _frameCounter++;
                _frameTime -= _deltaTime;
            }
        }
    }
}
